<!DOCTYPE html>

<html>
<head>
  <title>routes.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div id="container">
    <div id="background"></div>
    
      <ul id="jump_to">
        <li>
          <a class="large" href="javascript:void(0);">Jump To &hellip;</a>
          <a class="small" href="javascript:void(0);">+</a>
          <div id="jump_wrapper">
          <div id="jump_page">
            
              
              <a class="source" href="configure.html">
                configure.coffee
              </a>
            
              
              <a class="source" href="dataParser.html">
                dataParser.coffee
              </a>
            
              
              <a class="source" href="database.html">
                database.coffee
              </a>
            
              
              <a class="source" href="logger.html">
                logger.coffee
              </a>
            
              
              <a class="source" href="mail.html">
                mail.coffee
              </a>
            
              
              <a class="source" href="messages.html">
                messages.coffee
              </a>
            
              
              <a class="source" href="routes.html">
                routes.coffee
              </a>
            
              
              <a class="source" href="settings.html">
                settings.coffee
              </a>
            
              
              <a class="source" href="settings.default.html">
                settings.default.coffee
              </a>
            
              
              <a class="source" href="twitter.html">
                twitter.coffee
              </a>
            
          </div>
        </li>
      </ul>
    
    <ul class="sections">
        
          <li id="title">
              <div class="annotation">
                  <h1>routes.coffee</h1>
              </div>
          </li>
        
        
        
        <li id="section-1">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-1">&#182;</a>
              </div>
              <h2>SERVER ROUTES</h2>

            </div>
            
        </li>
        
        
        <li id="section-2">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-2">&#182;</a>
              </div>
              <p>All server routes should be defined in this file!</p>
<p>Require models.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>countdown = require <span class="string">"./../assets/js/models/countdown.coffee"</span>
eggTimer = require <span class="string">"./../assets/js/models/eggTimer.coffee"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-3">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-3">&#182;</a>
              </div>
              <p>Require app modules.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>database = require <span class="string">"./database.coffee"</span>
dataParser = require <span class="string">"./dataParser.coffee"</span>
logger = require <span class="string">"./logger.coffee"</span>
mail = require <span class="string">"./mail.coffee"</span>
messages = require <span class="string">"./messages.coffee"</span>
moment = require <span class="string">"moment"</span>
settings = require <span class="string">"./settings.coffee"</span>
twitter = require <span class="string">"./twitter.coffee"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-4">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-4">&#182;</a>
              </div>
              <h2>SET ROUTES</h2>

            </div>
            
        </li>
        
        
        <li id="section-5">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-5">&#182;</a>
              </div>
              <p>Bind all routes.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">set</span></span> = (app) -&gt;</pre></div></div>
            
        </li>
        
        
        <li id="section-6">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-6">&#182;</a>
              </div>
              <p>JSON data: get and set <a href="countdown.html">Countdown</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.get   <span class="string">"/db/countdown/:id"</span>, exports.countdownGet
    app.post  <span class="string">"/db/countdown"</span>,     exports.countdownPost
    app.put   <span class="string">"/db/countdown"</span>,     exports.countdownPut</pre></div></div>
            
        </li>
        
        
        <li id="section-7">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-7">&#182;</a>
              </div>
              <p>JSON data: get and set <a href="eggTimer.html">Egg Timers</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.get   <span class="string">"/db/eggtimer/:id"</span>, exports.eggTimerGet
    app.post  <span class="string">"/db/eggtimer"</span>,     exports.eggTimerPost</pre></div></div>
            
        </li>
        
        
        <li id="section-8">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-8">&#182;</a>
              </div>
              <p>Content pages.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.get   <span class="string">"/"</span>,              exports.indexPage
    app.get   <span class="string">"/edit/:id"</span>,      exports.indexPage
    app.get   <span class="string">"/about"</span>,         exports.aboutPage
    app.get   <span class="string">"/stats"</span>,         exports.statsPage</pre></div></div>
            
        </li>
        
        
        <li id="section-9">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-9">&#182;</a>
              </div>
              <p>Email processing.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.get   <span class="string">"/email/inbound"</span>, exports.emailInboundGet
    app.post  <span class="string">"/email/inbound"</span>, exports.emailInboundPost</pre></div></div>
            
        </li>
        
        
        <li id="section-10">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-10">&#182;</a>
              </div>
              <p>Special countdowns and features.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.get   <span class="string">"/egg"</span>,           exports.eggTimerPage
    app.get   <span class="string">"/eggtimer"</span>,      exports.eggTimerPage
    app.get   <span class="string">"/xmas"</span>,          exports.countdownPageChristmas
    app.get   <span class="string">"/christmas"</span>,     exports.countdownPageChristmas</pre></div></div>
            
        </li>
        
        
        <li id="section-11">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-11">&#182;</a>
              </div>
              <p>User generated countdowns.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    app.get   <span class="string">"/:id"</span>,           exports.countdownPage</pre></div></div>
            
        </li>
        
        
        <li id="section-12">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-12">&#182;</a>
              </div>
              <h2>CONTENT PAGES</h2>

            </div>
            
        </li>
        
        
        <li id="section-13">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-13">&#182;</a>
              </div>
              <p>The index / homepage.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">indexPage</span></span> = (req, res) -&gt;
    logRequest <span class="string">"indexPage"</span>

    renderPage req, res, <span class="string">"index"</span>,
        title: settings.General.appTitle</pre></div></div>
            
        </li>
        
        
        <li id="section-14">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-14">&#182;</a>
              </div>
              <p>The &quot;About&quot; page</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">aboutPage</span></span> = (req, res) -&gt;
    logRequest <span class="string">"aboutPage"</span>

    renderPage req, res, <span class="string">"about"</span>,
        title: messages.about</pre></div></div>
            
        </li>
        
        
        <li id="section-15">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-15">&#182;</a>
              </div>
              <p>The &quot;Stats&quot; page</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">statsPage</span></span> = (req, res) -&gt;
    logRequest <span class="string">"statsPage"</span>

    database.countCountdown <span class="literal">null</span>, (err, result) -&gt;
        renderPage req, res, <span class="string">"stats"</span>,
            title: messages.stats
            count: result</pre></div></div>
            
        </li>
        
        
        <li id="section-16">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-16">&#182;</a>
              </div>
              <h2>COUNTDOWNS AND UTILTY PAGES</h2>

            </div>
            
        </li>
        
        
        <li id="section-17">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-17">&#182;</a>
              </div>
              <p>A special &quot;Egg Timer&quot; page.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">eggTimerPage</span></span> = (req, res) -&gt;
    logRequest <span class="string">"eggTimerPage"</span>, req.params.id

    renderPage req, res, <span class="string">"eggTimer"</span>,
        header: messages.eggTimer,
        title: messages.eggTimer + <span class="string">" - "</span> + settings.General.appTitle</pre></div></div>
            
        </li>
        
        
        <li id="section-18">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-18">&#182;</a>
              </div>
              <p>A special &quot;Time to Christmas&quot; page.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">countdownPageChristmas</span></span> = (req, res) -&gt;
    logRequest <span class="string">"countdownPageChristmas"</span>, req.params.id</pre></div></div>
            
        </li>
        
        
        <li id="section-19">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-19">&#182;</a>
              </div>
              <p>The &quot;Countdown view&quot; page</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">countdownPage</span></span> = (req, res) -&gt;
    logRequest <span class="string">"countdownPage"</span>, req.params.id

    database.getCountdown {friendlyUrl: req.params.id}, (err, result) -&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> err? <span class="keyword">and</span> result? <span class="keyword">and</span> result.length &gt; <span class="number">0</span>
            now = moment()
            cd = <span class="keyword">new</span> countdown result[<span class="number">0</span>]

            title = cd.get <span class="string">"title"</span>
            playSound = cd.getBool <span class="string">"playSound"</span>

            viewCount = cd.get <span class="string">"viewCount"</span>
            cd.set <span class="string">"viewCount"</span>, viewCount + <span class="number">1</span>

            creationDate = moment cd.get <span class="string">"creationDate"</span>
            targetDate = moment cd.get <span class="string">"targetDate"</span>
            duration = moment.duration targetDate.valueOf() - now.valueOf()</pre></div></div>
            
        </li>
        
        
        <li id="section-20">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-20">&#182;</a>
              </div>
              <p>Editing will be enabled only if IP is the same as the creator&#39;s IP and
if the countdown was created less than a few hours ago.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            editEnabled = now.isBefore(targetDate) <span class="keyword">and</span> canEditCountdown(cd, req)

            renderPage req, res, <span class="string">"countdown"</span>,
                title: title + <span class="string">" - "</span> + settings.General.appTitle
                cd: cd
                creationDate: creationDate
                targetDate: targetDate
                editEnabled: editEnabled
                duration: duration
                playSound: playSound
                onEndMessage: dataParser.htmlLinkify cd.attributes.onEndMessage</pre></div></div>
            
        </li>
        
        
        <li id="section-21">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-21">&#182;</a>
              </div>
              <p>After all done, increment the viewCount on the database.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            database.updateCountdown cd
        <span class="keyword">else</span>
            res.status(<span class="number">404</span>)
            renderPage req, res, <span class="string">"404"</span>,
                title: <span class="string">"Countdown does not exist!"</span>
                friendlyUrl: req.params.id</pre></div></div>
            
        </li>
        
        
        <li id="section-22">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-22">&#182;</a>
              </div>
              <h2>COUNTDOWN JSON DATA</h2>

            </div>
            
        </li>
        
        
        <li id="section-23">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-23">&#182;</a>
              </div>
              <p>Get a <a href="countdown.html">Countdown</a> with the specified ID.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">countdownGet</span></span> = (req, res) -&gt;
    logRequest <span class="string">"countdownGet"</span>, req.params.id

    database.getCountdown {friendlyUrl: req.params.id}, (err, result) -&gt;
        <span class="keyword">if</span> result? <span class="keyword">and</span> <span class="keyword">not</span> err?
            res.json result
        <span class="keyword">else</span>
            sendErrorResponse res, <span class="string">"Countdown GET"</span>, err</pre></div></div>
            
        </li>
        
        
        <li id="section-24">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-24">&#182;</a>
              </div>
              <p>Post a new <a href="countdown.html">Countdown</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">countdownPost</span></span> = (req, res) -&gt;
    logRequest <span class="string">"countdownPost"</span>, req.body.title + <span class="string">" @ "</span> + req.body.targetDate</pre></div></div>
            
        </li>
        
        
        <li id="section-25">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-25">&#182;</a>
              </div>
              <p>Create a new countdown model and prepare it for DB insertion.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    cd = <span class="keyword">new</span> countdown req.body
    prepareCountdownForDatabase cd, req</pre></div></div>
            
        </li>
        
        
        <li id="section-26">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-26">&#182;</a>
              </div>
              <p>Save to the database. In case of error, send the error details back to the client.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    database.insertCountdown cd, (err, result) -&gt;
        <span class="keyword">if</span> err?
            sendErrorResponse res, <span class="string">"Countdown POST"</span>, err
        <span class="keyword">else</span>
            result = result[<span class="number">0</span>] <span class="keyword">if</span> result.length &gt; <span class="number">0</span>
            res.json result</pre></div></div>
            
        </li>
        
        
        <li id="section-27">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-27">&#182;</a>
              </div>
              <p>Recreate the model and trigger the after creation events.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            cd = <span class="keyword">new</span> countdown result
            afterCountdownCreated cd</pre></div></div>
            
        </li>
        
        
        <li id="section-28">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-28">&#182;</a>
              </div>
              <p>Update an existing <a href="countdown.html">Countdown</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">countdownPut</span></span> = (req, res) -&gt;
    logRequest <span class="string">"countdownPut"</span>, req.body.id

    database.getCountdown {id: req.body.id}, (err, result) -&gt;
        <span class="keyword">if</span> <span class="keyword">not</span> result? <span class="keyword">or</span> err?
            sendErrorResponse res, <span class="string">"Countdown PUT"</span>, err
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-29">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-29">&#182;</a>
              </div>
              <p>Instance a <a href="countdown.html">Countdown</a> model and
check permissions to edit the specified model.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        cd = <span class="keyword">new</span> countdown result

        <span class="keyword">if</span> <span class="keyword">not</span> canEditCountdown cd, req
            res.sendErrorResponse res, <span class="number">403</span>, <span class="string">"You don't have permission to edit this countdown."</span>
            <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-30">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-30">&#182;</a>
              </div>
              <p>Update values and remove uneditable countdown attributes.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>        cd.set req.body
        cd.del <span class="string">"friendlyUrl"</span>
        cd.del <span class="string">"viewCount"</span>

        database.updateCountdown cd, (err2, result2) -&gt;
            <span class="keyword">if</span> err2?
                sendErrorResponse res, <span class="string">"Countdown PUT"</span>, err2
            <span class="keyword">else</span>
                res.json cd.attributes</pre></div></div>
            
        </li>
        
        
        <li id="section-31">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-31">&#182;</a>
              </div>
              <h2>EGG TIMER JSON DATA</h2>

            </div>
            
        </li>
        
        
        <li id="section-32">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-32">&#182;</a>
              </div>
              <p>Get a <a href="eggTimer.html">Egg Timer</a> with the specified ID.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">eggTimerGet</span></span> = (req, res) -&gt;
    logRequest <span class="string">"eggTimerGet"</span>, req.params.id

    database.getEggTimer {friendlyUrl: req.params.id}, (err, result) -&gt;
        <span class="keyword">if</span> result? <span class="keyword">and</span> <span class="keyword">not</span> err?
            res.json result
        <span class="keyword">else</span>
            sendErrorResponse res, <span class="string">"EggTimer GET"</span>, err</pre></div></div>
            
        </li>
        
        
        <li id="section-33">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-33">&#182;</a>
              </div>
              <p>Post a new <a href="eggTimer.html">Egg Timer</a>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">eggTimerPost</span></span> = (req, res) -&gt;
    logRequest <span class="string">"eggTimerPost"</span>, req.body.size + <span class="string">" "</span> + req.body.yolk</pre></div></div>
            
        </li>
        
        
        <li id="section-34">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-34">&#182;</a>
              </div>
              <p>Create a new egg timer model and prepare it for DB insertion.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    egg = <span class="keyword">new</span> eggTimer req.body
    prepareModelForDatabase egg, req</pre></div></div>
            
        </li>
        
        
        <li id="section-35">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-35">&#182;</a>
              </div>
              <p>Save to the database. In case of error, send the error details back to the client.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    database.insertEggTimer egg, (err, result) -&gt;
        <span class="keyword">if</span> err?
            sendErrorResponse res, <span class="string">"EggTimer POST"</span>, err
        <span class="keyword">else</span>
            result = result[<span class="number">0</span>] <span class="keyword">if</span> result.length &gt; <span class="number">0</span>
            res.json result</pre></div></div>
            
        </li>
        
        
        <li id="section-36">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-36">&#182;</a>
              </div>
              <h2>EMAIL METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-37">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-37">&#182;</a>
              </div>
              <p>Inbound emails should be send with POST, so redirect the client to the
homepage in case it&#39;s a GET request.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">emailInboundGet</span></span> = (req, res) -&gt;
    logRequest <span class="string">"emailInboundGet"</span>

    res.redirect settings.General.appUrl</pre></div></div>
            
        </li>
        
        
        <li id="section-38">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-38">&#182;</a>
              </div>
              <p>Parses inbound emails and generate a new countdown.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>exports.<span class="function"><span class="title">emailInboundPost</span></span> = (req, res) -&gt;
    logRequest <span class="string">"emailInboundPost"</span>, req.body</pre></div></div>
            
        </li>
        
        
        <li id="section-39">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-39">&#182;</a>
              </div>
              <p>Get message from request body. If message wasn&#39;t found then
stop processing and log an error.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="keyword">try</span>
        body = req.body.mandrill_events.pop()
        text = body.msg.text
    <span class="keyword">catch</span> ex
        logger.error <span class="string">"Routes.emailInboundGet"</span>, <span class="string">"Could not find email message. Abort!"</span>, body
        <span class="keyword">return</span>

    cd = <span class="keyword">new</span> countdown()</pre></div></div>
            
        </li>
        
        
        <li id="section-40">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-40">&#182;</a>
              </div>
              <p>Make sure message was parsed successfully before proceeding.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="keyword">if</span> <span class="keyword">not</span> dataParser.textToCountdown cd, text
        <span class="keyword">if</span> settings.General.debug
            logger.warn <span class="string">"Routes.emailInboundGet"</span>, <span class="string">"Could not parse messsage to generate countdown."</span>, msg.text
        <span class="keyword">return</span></pre></div></div>
            
        </li>
        
        
        <li id="section-41">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-41">&#182;</a>
              </div>
              <p>Prepare countdown.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    prepareCountdownForDatabase cd, req</pre></div></div>
            
        </li>
        
        
        <li id="section-42">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-42">&#182;</a>
              </div>
              <p>Force set creation source to <code>email</code>.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    cd.set <span class="string">"createdFrom"</span>, <span class="string">"email"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-43">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-43">&#182;</a>
              </div>
              <p>Append countdown data (title, creator name, etc) based on the incoming message.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    cd.set <span class="string">"title"</span>, msg[<span class="string">"subject"</span>] <span class="keyword">if</span> msg[<span class="string">"subject"</span>]? <span class="keyword">and</span> msg[<span class="string">"subject"</span>] <span class="keyword">isnt</span> <span class="string">""</span>
    cd.set <span class="string">"creatorName"</span>, msg[<span class="string">"from_name"</span>] <span class="keyword">if</span> msg[<span class="string">"from_name"</span>]? <span class="keyword">and</span> msg[<span class="string">"from_name"</span>] <span class="keyword">isnt</span> <span class="string">""</span>
    cd.set <span class="string">"creatorEmail"</span>, msg[<span class="string">"from_email"</span>] <span class="keyword">if</span> msg[<span class="string">"from_email"</span>]? <span class="keyword">and</span> msg[<span class="string">"from_email"</span>] <span class="keyword">isnt</span> <span class="string">""</span></pre></div></div>
            
        </li>
        
        
        <li id="section-44">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-44">&#182;</a>
              </div>
              <p>Save to the database. In case of error, send the error details back to the client.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    database.insertCountdown cd, (err, result) -&gt;
        <span class="keyword">if</span> err?
            sendErrorResponse res, <span class="string">"EmailInbound POST"</span>, err
        <span class="keyword">else</span>
            result = result[<span class="number">0</span>] <span class="keyword">if</span> result.length &gt; <span class="number">0</span>
            res.send <span class="string">"OK"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-45">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-45">&#182;</a>
              </div>
              <p>Recreate the model and trigger the after creation events.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>            cd = <span class="keyword">new</span> countdown result
            afterCountdownCreated cd</pre></div></div>
            
        </li>
        
        
        <li id="section-46">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-46">&#182;</a>
              </div>
              <h2>COUNTDOWN HELPER METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-47">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-47">&#182;</a>
              </div>
              <p>Prepare the passed <a href="countdown.html">Countdown</a> model for database insertion.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">prepareCountdownForDatabase</span></span> = (cd, req) -&gt;
    prepareModelForDatabase cd, req
    targetDate = moment cd.get <span class="string">"targetDate"</span>
    cd.set <span class="string">"targetDate"</span>, targetDate.format <span class="string">"YYYY-MM-DD HH:mm:ss Z"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-48">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-48">&#182;</a>
              </div>
              <p>Trigger actions after a countdown has been created on the database. For example
post to Twitter or send an email to the creator. If countdown is private, skip this step.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">afterCountdownCreated</span></span> = (cd) -&gt;
    <span class="keyword">return</span> <span class="keyword">if</span> cd.getBool <span class="string">"isPrivate"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-49">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-49">&#182;</a>
              </div>
              <p>Get necessary countdown attributes.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    creatorName = cd.get <span class="string">"creatorName"</span>
    creatorEmail = cd.get <span class="string">"creatorEmail"</span>
    createdFrom = cd.get <span class="string">"createdFrom"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-50">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-50">&#182;</a>
              </div>
              <p>Post to Twitter only if creator name is set.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="keyword">if</span> creatorName? <span class="keyword">and</span> creatorName <span class="keyword">isnt</span> <span class="string">""</span>
        twitter.postCountdown cd</pre></div></div>
            
        </li>
        
        
        <li id="section-51">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-51">&#182;</a>
              </div>
              <p>If created from email, reply to the user.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="keyword">if</span> createdFrom <span class="keyword">is</span> <span class="string">"email"</span> <span class="keyword">and</span> creatorEmail? <span class="keyword">and</span> creatorEmail <span class="keyword">isnt</span> <span class="string">""</span>
        mail.send cd, messages.countdownCreated, creatorEmail</pre></div></div>
            
        </li>
        
        
        <li id="section-52">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-52">&#182;</a>
              </div>
              <p>Check if user has permissions to edit a countdown, by check the client IP
and the time when the counddown was created.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">canEditCountdown</span></span> = (cd, req) -&gt;
    now = moment()
    creatorIP = cd.get <span class="string">"creatorIP"</span>
    creationDate = moment cd.get <span class="string">"creationDate"</span>
    <span class="keyword">return</span> (moment.duration(now.valueOf() - creationDate.valueOf()).milliseconds() &lt; <span class="number">10800000</span>) <span class="keyword">and</span> (creatorIP <span class="keyword">is</span> getClientIP req)</pre></div></div>
            
        </li>
        
        
        <li id="section-53">
            <div class="annotation">
              
              <div class="pilwrap for-h2">
                <a class="pilcrow" href="#section-53">&#182;</a>
              </div>
              <h2>GENERAL HELPER METHODS</h2>

            </div>
            
        </li>
        
        
        <li id="section-54">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-54">&#182;</a>
              </div>
              <p>Prepare the passed model for database insertion.
This will set some of its non-editable attributes like creation date and creator IP.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">prepareModelForDatabase</span></span> = (model, req) -&gt;
    model.set <span class="string">"creationDate"</span>, moment().format <span class="string">"YYYY-MM-DD HH:mm:ss Z"</span>
    model.set <span class="string">"creatorIP"</span>, getClientIP req
    model.set <span class="string">"createdFrom"</span>, getClientDevice req</pre></div></div>
            
        </li>
        
        
        <li id="section-55">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-55">&#182;</a>
              </div>
              <p>Helper to render pages.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">renderPage</span></span> = (req, res, file, options) -&gt;
    options.device = getClientDevice req
    options.header = settings.General.pageHeader <span class="keyword">if</span> <span class="keyword">not</span> options.header? <span class="keyword">or</span> options.header <span class="keyword">is</span> <span class="string">""</span>
    res.render file, options</pre></div></div>
            
        </li>
        
        
        <li id="section-56">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-56">&#182;</a>
              </div>
              <p>Get the client&#39;s device identifier string based on the user agent.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">getClientDevice</span></span> = (req) -&gt;
    ua = req.headers[<span class="string">"user-agent"</span>]</pre></div></div>
            
        </li>
        
        
        <li id="section-57">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-57">&#182;</a>
              </div>
              <p>Find mobile devices.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="keyword">if</span> ua.indexOf(<span class="string">"Windows Phone 8"</span>) &gt; <span class="number">0</span>
        <span class="keyword">return</span> <span class="string">"wp8"</span>
    <span class="keyword">if</span> ua.indexOf(<span class="string">"Windows Phone"</span>) &gt; <span class="number">0</span>
        <span class="keyword">return</span> <span class="string">"wp"</span>
    <span class="keyword">if</span> ua.indexOf(<span class="string">"iPhone5"</span>) &gt; <span class="number">0</span>
        <span class="keyword">return</span> <span class="string">"iphone5"</span>
    <span class="keyword">if</span> ua.indexOf(<span class="string">"iPhone4"</span>) &gt; <span class="number">0</span>
        <span class="keyword">return</span> <span class="string">"iphone4"</span>
    <span class="keyword">if</span> ua.indexOf(<span class="string">"iPhone"</span>) &gt; <span class="number">0</span>
        <span class="keyword">return</span> <span class="string">"iphone"</span>
    <span class="keyword">if</span> ua.indexOf(<span class="string">"Android 4"</span>) &gt; <span class="number">0</span>
        <span class="keyword">return</span> <span class="string">"android4"</span>
    <span class="keyword">if</span> ua.indexOf(<span class="string">"Android"</span>) &gt; <span class="number">0</span>
        <span class="keyword">return</span> <span class="string">"android"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-58">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-58">&#182;</a>
              </div>
              <p>Return default desktop value if no mobile device found on user agent.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre>    <span class="keyword">return</span> <span class="string">"desktop"</span></pre></div></div>
            
        </li>
        
        
        <li id="section-59">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-59">&#182;</a>
              </div>
              <p>Get the client / browser IP.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">getClientIP</span></span> = (req) -&gt;
    <span class="keyword">try</span>
        xfor = req.header(<span class="string">"X-Forwarded-For"</span>)
        <span class="keyword">if</span> xfor? <span class="keyword">and</span> xfor <span class="keyword">isnt</span> <span class="string">""</span>
            ip = xfor.split(<span class="string">","</span>)[<span class="number">0</span>]
        <span class="keyword">else</span>
            ip = req.connection.remoteAddress
    <span class="keyword">catch</span> ex
        ip = req.connection.remoteAddress
    <span class="keyword">return</span> ip</pre></div></div>
            
        </li>
        
        
        <li id="section-60">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-60">&#182;</a>
              </div>
              <p>When the server can&#39;t return a valid result,
send an error response with status code 500.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">sendErrorResponse</span></span> = (res, method, message) -&gt;
    message = JSON.stringify message
    res.statusCode = <span class="number">500</span>
    res.send <span class="string">"Error: <span class="subst">#{method}</span> - <span class="subst">#{message}</span>"</span>
    logger.error <span class="string">"HTTP Error"</span>, method, message</pre></div></div>
            
        </li>
        
        
        <li id="section-61">
            <div class="annotation">
              
              <div class="pilwrap ">
                <a class="pilcrow" href="#section-61">&#182;</a>
              </div>
              <p>Log the request to the console if <code>debug</code> is true.</p>

            </div>
            
            <div class="content"><div class='highlight'><pre><span class="function"><span class="title">logRequest</span></span> = (method, params) -&gt;
    <span class="keyword">if</span> settings.General.debug
        <span class="keyword">if</span> params?
            console.log <span class="string">"Request"</span>, method, params
        <span class="keyword">else</span>
            console.log <span class="string">"Request"</span>, method</pre></div></div>
            
        </li>
        
    </ul>
  </div>
</body>
</html>
